package com.sponsorpay.sdk.android.publisher.currency;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Toast;
import com.sponsorpay.sdk.android.SponsorPay;
import com.sponsorpay.sdk.android.credentials.SPCredentials;
import com.sponsorpay.sdk.android.publisher.AbstractConnector;
import com.sponsorpay.sdk.android.publisher.AsyncRequest;
import com.sponsorpay.sdk.android.publisher.SponsorPayPublisher;
import com.sponsorpay.sdk.android.publisher.SponsorPayPublisher.UIStringIdentifier;
import com.sponsorpay.sdk.android.utils.SponsorPayBaseUrlProvider;
import com.sponsorpay.sdk.android.utils.SponsorPayLogger;
import com.sponsorpay.sdk.android.utils.StringUtils;
import com.sponsorpay.sdk.android.utils.UrlBuilder;
import java.util.Map;

public class VirtualCurrencyConnector extends AbstractConnector<SPCurrencyServerListener>
{
  private static final String STATE_LATEST_TRANSACTION_ID_KEY_PREFIX = "STATE_LATEST_CURRENCY_TRANSACTION_ID_";
  private static final String STATE_LATEST_TRANSACTION_ID_KEY_SEPARATOR = "_";
  private static final String URL_PARAM_KEY_LAST_TRANSACTION_ID = "ltid";
  private static final String URL_PARAM_VALUE_NO_TRANSACTION = "NO_TRANSACTION";
  private static final String VCS_URL_KEY = "vcs";
  private static boolean showToastNotification = true;
  private String mCurrency;
  private boolean mShouldShowNotification;

  public VirtualCurrencyConnector(Context paramContext, String paramString, SPCurrencyServerListener paramSPCurrencyServerListener)
  {
    super(paramContext, paramString, paramSPCurrencyServerListener);
  }

  public static String fetchLatestTransactionId(Context paramContext, String paramString)
  {
    SPCredentials localSPCredentials = SponsorPay.getCredentials(paramString);
    return paramContext.getSharedPreferences("SponsorPayPublisherState", 0).getString(generatePreferencesLatestTransactionIdKey(localSPCredentials.getAppId(), localSPCredentials.getUserId()), "NO_TRANSACTION");
  }

  private String fetchLatestTransactionIdForCurrentAppAndUser()
  {
    return fetchLatestTransactionId(this.mContext, this.mCredentials.getCredentialsToken());
  }

  private static String generatePreferencesLatestTransactionIdKey(String paramString1, String paramString2)
  {
    return "STATE_LATEST_CURRENCY_TRANSACTION_ID_" + paramString1 + "_" + paramString2;
  }

  private void saveLatestTransactionIdForCurrentUser(String paramString)
  {
    this.mContext.getSharedPreferences("SponsorPayPublisherState", 0).edit().putString(generatePreferencesLatestTransactionIdKey(this.mCredentials.getAppId(), this.mCredentials.getUserId()), paramString).commit();
  }

  public static void shouldShowToastNotification(boolean paramBoolean)
  {
    showToastNotification = paramBoolean;
  }

  public void fetchDeltaOfCoins()
  {
    fetchDeltaOfCoinsForCurrentUserSinceTransactionId(null);
  }

  public void fetchDeltaOfCoinsForCurrentUserSinceTransactionId(String paramString)
  {
    if (StringUtils.nullOrEmpty(paramString))
      paramString = fetchLatestTransactionIdForCurrentAppAndUser();
    String[] arrayOfString1 = { "ltid", "timestamp" };
    String[] arrayOfString2 = new String[2];
    arrayOfString2[0] = paramString;
    arrayOfString2[1] = getCurrentUnixTimestampAsString();
    Map localMap = UrlBuilder.mapKeysToValues(arrayOfString1, arrayOfString2);
    if (this.mCustomParameters != null)
      localMap.putAll(this.mCustomParameters);
    String str = UrlBuilder.newBuilder(SponsorPayBaseUrlProvider.getBaseUrl("vcs"), this.mCredentials).addExtraKeysValues(localMap).addScreenMetrics().buildUrl();
    SponsorPayLogger.d(getClass().getSimpleName(), "Delta of coins request will be sent to URL + params: " + str);
    AsyncRequest localAsyncRequest = new AsyncRequest(str, this);
    this.mShouldShowNotification = showToastNotification;
    localAsyncRequest.execute(new Void[0]);
  }

  public void onAsyncRequestComplete(AsyncRequest paramAsyncRequest)
  {
    String str = getClass().getSimpleName();
    Object[] arrayOfObject = new Object[3];
    arrayOfObject[0] = Integer.valueOf(paramAsyncRequest.getHttpStatusCode());
    arrayOfObject[1] = paramAsyncRequest.getResponseBody();
    arrayOfObject[2] = paramAsyncRequest.getResponseSignature();
    SponsorPayLogger.d(str, String.format("Currency Server Response, status code: %d, response body: %s, signature: %s", arrayOfObject));
    Object localObject;
    if (paramAsyncRequest.didRequestThrowError())
      localObject = new RequestErrorResponse();
    while (true)
    {
      ((CurrencyServerAbstractResponse)localObject).setResponseListener((SPCurrencyServerListener)this.mUserListener);
      ((CurrencyServerAbstractResponse)localObject).parseAndCallListener(this.mCredentials.getSecurityToken());
      return;
      localObject = new CurrencyServerDeltaOfCoinsResponse(this);
      ((CurrencyServerAbstractResponse)localObject).setResponseData(paramAsyncRequest.getHttpStatusCode(), paramAsyncRequest.getResponseBody(), paramAsyncRequest.getResponseSignature());
    }
  }

  public void onDeltaOfCoinsResponse(CurrencyServerDeltaOfCoinsResponse paramCurrencyServerDeltaOfCoinsResponse)
  {
    saveLatestTransactionIdForCurrentUser(paramCurrencyServerDeltaOfCoinsResponse.getLatestTransactionId());
    String str1;
    Object[] arrayOfObject;
    if ((paramCurrencyServerDeltaOfCoinsResponse.getDeltaOfCoins() > 0.0D) && (this.mShouldShowNotification))
    {
      str1 = SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.VCS_COINS_NOTIFICATION);
      arrayOfObject = new Object[2];
      arrayOfObject[0] = Double.valueOf(paramCurrencyServerDeltaOfCoinsResponse.getDeltaOfCoins());
      if (!StringUtils.notNullNorEmpty(this.mCurrency))
        break label88;
    }
    label88: for (String str2 = this.mCurrency; ; str2 = SponsorPayPublisher.getUIString(SponsorPayPublisher.UIStringIdentifier.VCS_DEFAULT_CURRENCY))
    {
      arrayOfObject[1] = str2;
      String str3 = String.format(str1, arrayOfObject);
      Toast.makeText(this.mContext, str3, 1).show();
      return;
    }
  }

  public VirtualCurrencyConnector setCurrency(String paramString)
  {
    this.mCurrency = paramString;
    return this;
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.sponsorpay.sdk.android.publisher.currency.VirtualCurrencyConnector
 * JD-Core Version:    0.6.2
 */